package o2o.utils;

import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

//导出Excel工具类
public class ExcelExportTools {

    /**
     * 创建sheet
     * @param workbook excel 对象
     * @param sheetName 新建sheet
     * @param index sheet 序列 从0开始
     */
    public static WritableSheet createSheet(WritableWorkbook workbook,String sheetName,int index){
        if(null == workbook){
            return null;
        }
        return  workbook.createSheet(sheetName,index);
    }

    /**
     * 写入文件 Excel
     * @param sheet sheet 文件对象
     * @param columnIndex 列
     * @param rowIndex //行
     * @param obj //写入对象
     * @throws WriteException
     */
    public static void createLabel(WritableSheet sheet, int columnIndex, int rowIndex, Object obj) throws Exception {
        if(null == sheet){
            throw  new RuntimeException();
        }
        if(null == obj){
            return ;
        }
        if(obj instanceof Integer){
            //int 类型
            sheet.addCell(new jxl.write.Number(columnIndex, rowIndex, Integer.parseInt(obj.toString())));
        }else if(obj instanceof Double){
            //double 类型
            sheet.addCell(new jxl.write.Number(columnIndex, rowIndex, Double.parseDouble(obj.toString())));
        }else if(obj instanceof Long){
            //long 类型
            sheet.addCell(new jxl.write.Number(columnIndex, rowIndex, Long.parseLong(obj.toString())));
        }else{
            //string 类型
            sheet.addCell(new Label(columnIndex, rowIndex, obj.toString()));
        }
    }

}
